package fenyu.personblob.savepath;

import fenyu.personblob.bean.databean.User;
import fenyu.personblob.bean.databean.blob.Blob;
import fenyu.personblob.bean.databean.blob.BlobComment;
import fenyu.personblob.bean.databean.logs.LogBean;
import fenyu.personblob.bean.observe.logobserve.LogSaveToFileObserve;
import fenyu.personblob.savepath.strategy.CreateLogStrategy;
import fenyu.personblob.single.SinglePathDoor;

import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import org.apache.commons.codec.binary.Base64;
import java.util.LinkedList;
import java.util.List;

/**
 * 门面
 * create at 2018/12/21 22:15
 *
 * @author 纷羽 <gzj0078@gmail.com>
 */
public class DefaultSaveDoor extends SaveDoor {

    private DefaultSavePath<Blob> blobSavePath = new DefaultSavePath<Blob>();

    private DefaultSavePath<BlobComment> blobCommentSavePath = new DefaultSavePath<BlobComment>();

    private DefaultSavePath<User> userSavePath = new DefaultSavePath<User>();

    private LinkedList<LogBean> logBeans = new LinkedList<LogBean>();

    private CreateLogStrategy createLogStrategy = new CreateLogStrategy();


    private static final String publicrsakey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCcAuWwwIJzfRGoq34MQJlujUx79A1RRSGeb+OfSCli90IosQtoHa4m4q7MA2hisw7AlLt62hskUMU7J+tgiWkmmRjnuNPfjWCOOVP7sT8kaezwCtNTadyKBLedYptLtg4ccfQ/lbvyfR25Imv9R2yLOK9jcHC+xluKGLLC/qQocQIDAQAB";

    private static final String privatersakey = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAJwC5bDAgnN9EairfgxAmW6NTHv0DVFFIZ5v459IKWL3QiixC2gdribirswDaGKzDsCUu3raGyRQxTsn62CJaSaZGOe409+NYI45U/uxPyRp7PAK01Np3IoEt51im0u2Dhxx9D+Vu/J9Hbkia/1HbIs4r2NwcL7GW4oYssL+pChxAgMBAAECgYABVn0FwQDdYKswGRhhnoQyUH/haDxdAAzDwQ3gpcvFD32/v5TrdaWyq6r4EOdqQULVH/Fjok8tRowKb9My5rrCGIQlO9mkYkW89o86+lPganxD3yTF+4mXmgZT/78jE9qA+5UwPd3m3kHbwpinYZ+HuxU6tjullAp+QcYNuKlUiQJBAOV7QaczUqS5F0zuUFOw7+z+LfInJXDdczol2QByMDbiB8mMshwP/bsF8Y4oEMoNmV59tTli0+VPBrSZ0oahmf8CQQCuCiznUhhV7CPiSaNeE8m7e841iCLmDvf9noLBTSxa6+1xqiNQnCI8wFAG3eX8gAlChn//UWt6Edf+1NeLkt2PAkBJiw3IUtdaeQLxobJWj+7kSbVZ1JfYKvBBo11/Ghk/jYKnYhK2kZ1kMYxvXYG7703GjYzlnjmAVKo2eNGSWfnrAkEAo7b8sIkgTUs6IjalD+1LWirHOsIaGhHApHoEg/Olos3GaaLjX4PQDRfW7+6y1shWbZb6NuGPWaAJcDUloa0A7wJAEjjh6PpVVqJUnCd3BVPxuta7PdNKgnLVQFPoyA2rKbbdVeAZijN3t33e8eYLwlSRL1OV7vbwvwbHQ4z8ddrwgg==";

    private KeyPair KEY_PAIR = null;

    public DefaultSaveDoor() {
        User user = (User) SinglePathDoor.getSingleFuns().getAutoFactory().createUser("admin");
        user.setUsername("admin");
        user.setPassword("admin");
        user.setNickname("admin");
        userSavePath.save(user);

        //监听异常日志
        createLogStrategy.addObserve(new LogSaveToFileObserve());

        createlog(0, "初始化了默认管理员用户admin 密码admin");

        try {
            byte[] keyBytes;
            keyBytes = Base64.decodeBase64(publicrsakey);
            X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            PublicKey publicKey = keyFactory.generatePublic(keySpec);

            keyBytes = Base64.decodeBase64(privatersakey);
            PKCS8EncodedKeySpec keySpec2 = new PKCS8EncodedKeySpec(keyBytes);
            PrivateKey privateKey = keyFactory.generatePrivate(keySpec2);
            KEY_PAIR = new KeyPair(publicKey, privateKey);
        } catch (Exception e) {

        }

    }

    @Override
    public DefaultSavePath<Blob> getBlobSavePath() {
        return blobSavePath;
    }

    @Override
    public DefaultSavePath<BlobComment> getBlobCommentSavePath() {
        return blobCommentSavePath;
    }

    @Override
    public DefaultSavePath<User> getUserSavePath() {
        return userSavePath;
    }

    @Override
    public List<LogBean> getlog() {
        return logBeans;
    }

    @Override
    public void createlog(Integer type, String log) {
        LogBean logBean = (LogBean) createLogStrategy.todo(type, log);
        System.out.println(logBean);
        logBeans.add(logBean);
    }

    @Override
    public String printAllLogs() {
        StringBuilder stringBuilder = new StringBuilder();
        for (LogBean l : logBeans) {
            stringBuilder.append(l.toString() + "\n");
        }
        return stringBuilder.toString();
    }

    @Override
    public KeyPair getKEY_PAIR() {
        return KEY_PAIR;
    }
}
